package com.xdq.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.xdq.commonutils.JwtUtils;import com.xdq.commonutils.R;
import com.xdq.serviceedu.client.MemberClient;
import com.xdq.serviceedu.entity.EduComment;import com.xdq.serviceedu.entity.frontvo.CommentVo;import com.xdq.serviceedu.service.EduCommentService;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-09-26
 */
@RestController
@RequestMapping("/eduservice/comment")
@CrossOrigin
public class EduCommentController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private MemberClient memberClient;

    // 查询课程评论
    // TODO 考虑做分页
    @GetMapping("getCourseComment/{courseId}")
    public R getCourseComment(@PathVariable String courseId) {
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        List<EduComment> list = commentService.list(wrapper);
        return R.ok().data("comments", list);
    }

    // 添加评论
    // 需要前端：
    @PutMapping("addComment")
    public R addComment(@RequestBody CommentVo commentVo, HttpServletRequest request) {
        // 远程调用 获取用户昵称和头像
        R memberInfo = memberClient.getMemberInfo();
        Map<String, Object> data = memberInfo.getData();
        Map userInfo = (Map) data.get("userInfo");
        System.out.println("token: " + request.getHeader("token"));
        if (StringUtils.isEmpty(userInfo)) {
            return R.error().message("请先登录");
        }
        EduComment comment = new EduComment();
        comment.setContent(commentVo.getContent());
        comment.setCourseId(commentVo.getCourseId());
        comment.setTeacherId(commentVo.getTeacherId());
        comment.setMemberId((String) userInfo.get("id"));
        comment.setNickname((String) userInfo.get("nickname"));
        comment.setAvatar((String) userInfo.get("avatar"));
        //
        commentService.save(comment);
        return R.ok();
    }

    @GetMapping("fegin")
    public R testFegin(HttpServletRequest request) {
        return memberClient.getMemberInfo();
    }
}

